;(function() {
  var wHeight = getViewportSize().height,
    sHeight = getScrollSize().height,
    playing = false,
    t = null;
  var AutoReader = function(opt) {
    this.playBtn = opt.playBtn;
    this.sTopBtn = opt.sTopBtn;
    
    var _self = this;
    
    addEvent(this.sTopBtn, 'click', function() {
      window.scrollTo(0, 0);
      clearInterval(t);
      _self.playBtn.style.backgroundImage = 'url(img/play.jpg)';
      playing = false;
    });
    addEvent(window, 'scroll', function() {
      _self.sTopBtnShow.call(_self);
    });
    
    addEvent(this.playBtn, 'click', function() {
      _self.setAutoPlay.call(_self.playBtn);
    });
  };
  AutoReader.prototype = {
    setAutoPlay: function() {
      var sTop = getScrollOffset().top,
        _self = this;
      if (sHeight === wHeight + sTop) {
        return;
      }
      
      if (!playing) {
        t = setInterval(function() {
          // 重新赋值
          sTop = getScrollOffset().top;
          if (sHeight - 1 <= wHeight + sTop) {
            clearInterval(t);
            _self.style.backgroundImage = 'url(img/play.jpg)';
            playing = false;
          } else {
            window.scrollBy(0, 1);
            _self.style.backgroundImage = 'url(img/stop.jpg)';
            playing = true;
          }
        }, 8);
      } else {
        clearInterval(t);
        _self.style.backgroundImage = 'url(img/play.jpg)';
        playing = false;
      }
    },
    sTopBtnShow: function() {
      var sTop = getScrollOffset().top,
        sTopBtn = this.sTopBtn;
      sTopBtn.style.display = sTop ? 'block' : 'none';
    },
  };
  window.AutoReader = AutoReader;
})();
